package jie_yue;

import JDBC.jdbc_user;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class look extends JFrame {
    public look(int id) {
        setTitle("个人借阅浏览");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(500, 400);
        setLocation(500,200);
        setLayout(new BorderLayout());

        //创建表格
        String[] columns = {"书本编号", "借阅时间","归还时间"};
        DefaultTableModel tableModel = new DefaultTableModel(columns, 0);
        JTable JTable = new JTable(tableModel);
        JScrollPane jScrollPane = new JScrollPane(JTable);
        add(jScrollPane, BorderLayout.CENTER);

        loadDate(tableModel,id);

        setVisible(true);
    }

    //显示
    private void loadDate(DefaultTableModel tableModel, int id) {
        System.out.println(id);
        try{
            Integer ID = id;
            Connection conn = jdbc_user.getConnection();
            PreparedStatement pstm = conn.prepareStatement("select BOOK_ID,BORROW_DATE,EXPECT_RETURN_DATE from BORROW where STU_ID=? ORDER BY BOOK_ID ASC");
            pstm.setInt(1,ID);
            //写入
            ResultSet rs = pstm.executeQuery();
            while (rs.next()) {
                Object[] row = {
                        rs.getInt(1),
                        rs.getDate(2),
                        rs.getDate(3),
                };
                tableModel.addRow(row);
            }
            pstm.close();
            conn.close();
        } catch (SQLException e){
            showError("加载失败："+e.getMessage());
        }
    }

    //弹窗
    private void showError(String s) {
        JOptionPane.showMessageDialog(this,s,"错误",JOptionPane.ERROR_MESSAGE);
    }
}
